package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import ewms.bo.EwmsWasteCollectAddBo;
import ewms.bo.EwmsWasteCollectEditBo;
import ewms.bo.EwmsWasteCollectQueryBo;
import ewms.domain.EwmsWasteCollectDetail;
import ewms.domain.EwmsWasteDeclare;
import ewms.mapper.EwmsWasteCollectDetailMapper;
import ewms.mapper.EwmsWasteDeclareMapper;
import ewms.utils.EwmsUtils;
import ewms.common.exception.CustomException;
import ewms.common.utils.PageUtils;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.service.IEwmsWasteCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ewms.domain.EwmsWasteCollect;
import ewms.mapper.EwmsWasteCollectMapper;
import ewms.vo.EwmsWasteCollectVo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 危废收运Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-01-26
 */
@Service
public class EwmsWasteCollectServiceImpl extends ServiceImpl<EwmsWasteCollectMapper, EwmsWasteCollect> implements IEwmsWasteCollectService {

	@Autowired
	private EwmsWasteCollectDetailMapper collectDetailMapper;
	@Autowired
	private EwmsWasteDeclareMapper wasteDeclareMapper;

	@Override
	public EwmsWasteCollectVo queryById(Long wasteCollectId) {
		return getVoById(wasteCollectId, EwmsWasteCollectVo.class);
	}

	@Override
	public TableDataInfo<EwmsWasteCollectVo> selectPageWasteCollectList(EwmsWasteCollectQueryBo bo) {
		return PageUtils.buildDataInfo(baseMapper.selectPageWasteCollectList(PageUtils.buildPage(), bo));
	}

	@Override
	public List<EwmsWasteCollectVo> selectWasteCollectList(EwmsWasteCollectQueryBo bo) {
		return baseMapper.selectWasteCollectList(bo);
	}

	@Override
	public TableDataInfo<EwmsWasteCollectVo> queryPageList(EwmsWasteCollectQueryBo bo) {
		PagePlus<EwmsWasteCollect, EwmsWasteCollectVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsWasteCollectVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<EwmsWasteCollectVo> queryList(EwmsWasteCollectQueryBo bo) {
		return listVo(buildQueryWrapper(bo), EwmsWasteCollectVo.class);
	}

	private LambdaQueryWrapper<EwmsWasteCollect> buildQueryWrapper(EwmsWasteCollectQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<EwmsWasteCollect> lqw = Wrappers.lambdaQuery();
		lqw.between(params.get("beginCollectDate") != null && params.get("endCollectDate") != null,
			EwmsWasteCollect::getCollectDate, params.get("beginCollectDate"), params.get("endCollectDate"));
		lqw.like(StrUtil.isNotBlank(bo.getCreateBy()), EwmsWasteCollect::getCreateBy, bo.getCreateBy());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(EwmsWasteCollectAddBo bo) {
		EwmsWasteCollect add = BeanUtil.toBean(bo, EwmsWasteCollect.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean collectByAddBo(EwmsWasteCollectAddBo bo) {
		EwmsWasteCollect add = BeanUtil.toBean(bo, EwmsWasteCollect.class);
		add.setCollectDate(EwmsUtils.handleSelectDate(add.getCollectDate()));
		validEntityBeforeSave(add);
		boolean saveFlag = save(add);
		Long wasteCollectId = add.getWasteCollectId();
		String wasteDeclareIds = bo.getWasteDeclareIds();
		if (StrUtil.isNotBlank(wasteDeclareIds)) {
			Arrays.stream(wasteDeclareIds.split(","))
				.forEach(wasteDeclareId -> {
					EwmsWasteDeclare ewmsWasteDeclare = wasteDeclareMapper.selectById(wasteDeclareIds);
					if (Objects.isNull(ewmsWasteDeclare)) {
						throw new CustomException("危废申报信息未找到");
					}
					EwmsWasteCollectDetail wasteCollectDetail = new EwmsWasteCollectDetail()
						.setWasteCollectId(wasteCollectId)
						.setWasteId(ewmsWasteDeclare.getWasteId())
						.setWasteDeclareId(Long.valueOf(wasteDeclareId))
						.setBatchNumber(ewmsWasteDeclare.getBatchNumber());
					collectDetailMapper.insert(wasteCollectDetail);
				});
		}
		return saveFlag;
	}

	@Override
	public Boolean updateByEditBo(EwmsWasteCollectEditBo bo) {
		EwmsWasteCollect update = BeanUtil.toBean(bo, EwmsWasteCollect.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(EwmsWasteCollect entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		List<EwmsWasteCollectDetail> collectDetailList = collectDetailMapper.selectBatchIds(ids);
		Set<Long> collectIdList = collectDetailList.stream().map(detail -> detail.getWasteCollectId()).collect(Collectors.toSet());
		int i = collectDetailMapper.deleteBatchIds(ids);
		if (i > 0) {
			collectIdList.stream()
				.filter(collectId -> collectDetailMapper.selectCount(new LambdaQueryWrapper<EwmsWasteCollectDetail>().eq(EwmsWasteCollectDetail::getWasteCollectId, collectId)) == 0)
				.forEach(collectId -> baseMapper.deleteById(collectId));
		}
		return SqlHelper.retBool(i);
	}
}
